#include "stdafx.h"

#include "ZippedResourceFile.h"

ZippedResourceFile::~ZippedResourceFile() 
{ 
	SAFE_DELETE(_pZipFile); 
}

bool ZippedResourceFile::Open()
{
	_pZipFile = TWF_NEW ZipFile;

    if (_pZipFile)
    {
		return _pZipFile->Init(_resFileName.c_str());
	}
	return false;	
}

int ZippedResourceFile::GetRawResourceSize(const Resource &r)
{
	int resourceNum = _pZipFile->Find(r.Name.c_str());

	if (resourceNum == -1)
		return -1;

	return _pZipFile->GetFileLen(resourceNum);
}

int ZippedResourceFile::GetRawResource(
	const Resource &r, 
	char *buffer)
{
	int size = 0;

	optional<int> resourceNum = _pZipFile->Find(r.Name.c_str());

	if (resourceNum.valid())
	{
		size = _pZipFile->GetFileLen(*resourceNum);
		_pZipFile->ReadFile(*resourceNum, buffer);
	}

	return size;	
}

int ZippedResourceFile::GetNumResources() const 
{ 
	return (_pZipFile == NULL) 
		? 0 
		: _pZipFile->GetNumFiles(); 
}

std::string ZippedResourceFile::GetResourceName(int num) const 
{ 
	std::string resName = "";

	if ((_pZipFile != NULL) && 
		(num >= 0) && 
		(num < _pZipFile->GetNumFiles()))
	{
		resName = _pZipFile->GetFilename(num); 
	}

	return resName;
}